package com.zyg.demo.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzyg.logger.JLog;
import com.zyg.demo.R;
import com.zyg.demo.adapter.PluginAdapter;
import com.zyg.demo.control.DividerGridItemDecoration;
import com.zyg.demo.control.DragControler;
import com.zyg.demo.control.FolderEditor;
import com.zyg.demo.control.SelfDragAndSwipeCallback;
import com.zyg.demo.entity.Plugin;
import com.zyg.demo.entity.PluginFolderView;
import com.zyg.demo.listener.Drag2FolderListener;
import com.zyg.demo.util.Constants;
import com.zyg.demo.util.DataHelper;
import com.zyg.demo.util.InitHelper;

public class MainActivity extends AppCompatActivity {

    RecyclerView recycler;

    PluginAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitHelper.init();
        setContentView(R.layout.activity_main);
        recycler = findViewById(R.id.recycler);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        recycler.setLayoutManager(layoutManager);
        recycler.addItemDecoration(new DividerGridItemDecoration(R.color.light_gray));
        DragControler controler = new DragControler(getResources().getColor(R.color.light_gray)
                , getResources().getColor(R.color.light_gray));
        recycler.addItemDecoration(controler);
        controler.setOnDrag2FolderListener(dragReleaseListener);
        mAdapter = new PluginAdapter();
        SelfDragAndSwipeCallback dragAndSwipeCallback = new SelfDragAndSwipeCallback(mAdapter);
        dragAndSwipeCallback.setDragListener(controler);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(dragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(recycler);
        mAdapter.enableDragItem(itemTouchHelper);

        mAdapter.setOnItemDragListener(controler);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                JLog.d("OnItemChildClick");
//                long current = System.currentTimeMillis();
                PluginFolderView folderView = view.findViewById(R.id.iv_plugin);
                Plugin plugin = mAdapter.getItem(position);
                if (plugin != null && plugin.type == Constants.TYPE_FOLDER) {
                    FolderEditor.newInstance(recycler, plugin, position).expand(folderView);
                }
            }
        });

        mAdapter.setNewData(DataHelper.createPluginList());
        recycler.setAdapter(mAdapter);
    }


    private Drag2FolderListener dragReleaseListener = new Drag2FolderListener() {
        @Override
        public void onDrag2Folder(boolean createFolder, final RecyclerView.ViewHolder source, final int fromPos,
                                  RecyclerView.ViewHolder target, final int toPos) {
//            JLog.d("createFolder = " + createFolder);
//            JLog.d("source = " + source);
//            JLog.d("fromPos = " + fromPos);
//            JLog.d("target = " + target);
//            JLog.d("toPos = " + toPos);
            /**
             * 必须强制setIsRecyclable(true)，否则可能remove失败
             * 但是也可能出问题，因为onRelease是在RecoverAnimation正在执行时候触发的
             * 还有一种解决方案是尝试监听RecoverAnimation的结束动画然后remove，需要自己
             * 定制ItemTouchHelper，同时实现效果也不太好
             *
             */
            PluginFolderView folderView = target.itemView.findViewById(R.id.iv_plugin);
            Plugin touchPlugin = mAdapter.getItem(fromPos);
            Plugin plugin = mAdapter.getItem(toPos);
            source.setIsRecyclable(true);
            source.itemView.setAlpha(0);
            mAdapter.remove(fromPos);
            if (plugin != null) {
                if (createFolder) {
                    Plugin tempPlugin = new Plugin();
                    tempPlugin.name = "文件夹";
                    tempPlugin.addPlugin(plugin);
                    tempPlugin.addPlugin(touchPlugin);
                    tempPlugin.type = Constants.TYPE_FOLDER;
                    tempPlugin.dragState = Constants.DRAW_STATE_INITIAL;
                    if (fromPos < toPos) {
                        mAdapter.setData(toPos - 1, tempPlugin);
                    } else {
                        mAdapter.setData(toPos, tempPlugin);
                    }
                } else {
                    plugin.addPlugin(touchPlugin);
                    plugin.dragState = Constants.DRAW_STATE_ADDING;
//                    mAdapter.notifyItemChanged(toPos);
                    folderView.setPlugin(plugin);
                }
            }

        }
    };

}
